package top.majuan.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.majuan.springboot3.base.PageBean;
import top.majuan.springboot3.base.R;
import top.majuan.springboot3.dao.TbSoftwaretypeMapper;
import top.majuan.springboot3.entity.TbSoftwaretype;
import top.majuan.springboot3.service.TbSoftwaretypeService;
import top.majuan.springboot3.vo.PageVo;

import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbSoftwaretypeServiceImpl implements TbSoftwaretypeService {
    private final TbSoftwaretypeMapper tbSoftwaretypeMapper;
    public TbSoftwaretype queryByTname(String tname) {
        QueryWrapper<TbSoftwaretype> wrapper = new QueryWrapper<>();
        wrapper.eq("tname",tname);
        TbSoftwaretype tbSoftwaretype = tbSoftwaretypeMapper.selectOne(wrapper);
        return tbSoftwaretype;
    }
    @Override
    public R<String> insert(TbSoftwaretype tbSoftwaretype) {
        TbSoftwaretype check = queryByTname((tbSoftwaretype.getTname()));
        if(check!=null){
            return R.fail("软件类型已存在",null);
        }
        int i = tbSoftwaretypeMapper.insert(tbSoftwaretype);
        return i==1?R.ok("新增成功",null):R.fail("新增失败",null);
    }
    @Override
    public R<List<TbSoftwaretype>> query(TbSoftwaretype tbSoftwaretype) {
        QueryWrapper<TbSoftwaretype> wrapper = new QueryWrapper<>();
        wrapper.like("tname",tbSoftwaretype.getTname());
        wrapper.like("intro",tbSoftwaretype.getIntro());

        List<TbSoftwaretype> list = tbSoftwaretypeMapper.selectList(wrapper);
        return R.ok(list);
    }
    @Override
    public R<PageVo<TbSoftwaretype>> queryPage(PageBean pageBean, TbSoftwaretype tbSoftwaretype) {
        IPage<TbSoftwaretype> page = new Page<>(pageBean.getPageNumber(),pageBean.getPageSize());
        QueryWrapper<TbSoftwaretype> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.hasText(tbSoftwaretype.getTname()),"tname",tbSoftwaretype.getTname());
        wrapper.like(StringUtils.hasText(tbSoftwaretype.getIntro()),"intro",tbSoftwaretype.getIntro());
        page = tbSoftwaretypeMapper.selectPage(page,wrapper);

        return R.ok(page);
    }
    @Override
    public R<TbSoftwaretype> queryByid(Integer id) {
        TbSoftwaretype tbSoftwaretype = tbSoftwaretypeMapper.selectById(id);
        return tbSoftwaretype == null?R.fail("查询失败",null) : R.ok(tbSoftwaretype);
    }

    @Override
    public R<String> update(TbSoftwaretype tbSoftwaretype) {
        TbSoftwaretype check = queryByTname((tbSoftwaretype.getTname()));
        if(check!=null && !check.getId().equals(tbSoftwaretype.getId())){
            return R.fail("软件类型已存在",null);
        }
        int i = tbSoftwaretypeMapper.updateById(tbSoftwaretype);
        return i==1?R.ok("修改成功",null):R.fail("修改失败",null);
    }
    @Override
    public R<String> delete(Integer id) {
        int i = tbSoftwaretypeMapper.deleteById(id);
        return i==1?R.ok("删除成功",null):R.fail("删除失败",null);
    }
}
